home *** CD-ROM | disk | FTP | other *** search
/ 3D Game Programming All in One / 3D Game Programming All in One Disc.iso / 3D2E / RESOURCES / CH6 / EMAGA6 BOOK CODE / main.cs
Text File  |  2006-09-21  |  6KB  |  172 lines

  1. $traceMode = false;
  2. $winConsole = false;
  3.  
  4.  
  5.  
  6. trace($traceMode);//leave this as is,change $traceMode above to true/false instead.
  7. EnableWinConsole($winConsole); // leave alone, change $winConsole instead.
  8. // send logging output to a windows console window
  9. //------------------------------------------------------------------------
  10. //  ./main.cs   v0.3
  11. //
  12. //  root main module for 3D2E emaga4 sample game
  13. //
  14. //  Copyright (c) 2003, 2006 by Kenneth C.  Finney.
  15. //------------------------------------------------------------------------
  16.  
  17. // ========================================================================
  18. // ========================= Initializations ==============================
  19. // ========================================================================
  20.  
  21. $usageFlag =  false;  //help won't be displayed unless the command line
  22.                       //switch ( -h ) is used
  23.  
  24. $logModeEnabled =  true; //track the logging state we set in the next line.
  25. SetLogMode(2);   // overwrites existing logfile & closes log file at exit.
  26.  
  27. // ========================================================================
  28. // ======================= Function Definitions ===========================
  29. // ========================================================================
  30.  
  31. function OnExit()
  32. //------------------------------------------------------------------------
  33. // This is called from the common code modules. Any last gasp exit
  34. // acticities we might want to perform can be put in this function.
  35. // We need to provide a stub to prevent warnings in the log file.
  36. //------------------------------------------------------------------------
  37. {
  38. }
  39.  
  40. function OnStart()
  41. //------------------------------------------------------------------------
  42. // This is called from the common code modules. Any last initial
  43. // acticvities we might want to perform can be put in this function.
  44. // We need to provide a stub to prevent warnings in the log file.
  45. //------------------------------------------------------------------------
  46. {
  47. }
  48.  
  49. function ParseArgs()
  50. //------------------------------------------------------------------------
  51. //  handle the command line arguments
  52. //
  53. //  this function is called from the common code
  54. // NOTE: some variables used in the function are globals (use the $)
  55. // because we need their values that are set inside this function to
  56. // also be accessible outside this function as well.
  57. //------------------------------------------------------------------------
  58. {
  59.   for(%i = 1; %i  < $Game::argc ; %i++) //loop thru all command line args
  60.   {
  61.     %currentarg    = $Game::argv[%i];   // get current arg from the list
  62.     %nextArgument       = $Game::argv[%i+1]; // get arg after the current one
  63.     %nextArgExists = $Game::argc-%i > 1;// if there *is* a next arg,note that
  64.     $logModeEnabled =  false;           // turn this off-let the args dictate
  65.                                 // if logging should be enabled.
  66.  
  67.  
  68.     switch$(%currentarg)
  69.     {
  70.       case "-dedicated":
  71.         $Server::Dedicated = true;
  72.         EnableWinConsole(true);
  73.         $argumentFlag[%i]++;
  74.  
  75.       case "-map":
  76.         $argumentFlag[%i]++;
  77.         if (%nextArgExists)
  78.         {
  79.            $mapArgument = %nextArgument;
  80.            $argumentFlag[%i+1]++;
  81.            %i++;
  82.         }
  83.         else
  84.            Error("Error: Missing argument. Usage: -map <filename>");
  85.  
  86.  
  87.       case "-?":       // the user wants command line help, so this cause the
  88.         $usageFlag = true;   // Usage function to be run, instead of the game
  89.         $argumentFlag[%i] = true;                // adjust the argument count
  90.  
  91.       case "-h":         // exactly the same as "-?"
  92.         $usageFlag = true;
  93.         $argumentFlag[%i] = true;
  94.  
  95.     }
  96.   }
  97. }
  98.  
  99. function Usage()
  100. //------------------------------------------------------------------------
  101. // Display the command line usage help
  102. //------------------------------------------------------------------------
  103. {
  104. //  NOTE: any logging entries are written to the file 'console.log'
  105.   Echo("\n\nemaga4 command line options:\n\n" @
  106.          " -h, -?              display this message\n" );
  107. }
  108.  
  109. function  LoadAddOns(%list)
  110. //------------------------------------------------------------------------
  111. // Exec each of the startup scripts for add-ons.
  112. //------------------------------------------------------------------------
  113. {
  114.   if (%list $= "")
  115.     return;
  116.   %list = NextToken(%list, token, ";");
  117.   LoadAddOns(%list);
  118.   Exec(%token @  "/main.cs");
  119. }
  120.  
  121. // ========================================================================
  122. // ================ Module Body - Inline Statements =======================
  123. // ========================================================================
  124. //  Parse the command line  arguments
  125. ParseArgs();
  126.  
  127. //  Either  display the help message or start the program.
  128. if  ($usageFlag)
  129. {
  130.   %saveWinConsole = $winConsole;
  131.   EnableWinConsole(false);// send logging output to a windows console window
  132.   Usage();
  133.   EnableWinConsole(%saveWinConsole);//restore windows console mode
  134.   Quit();
  135. }
  136. else
  137. {
  138.  
  139.   //  scan argument list, and log an Error message for each unused argument
  140.   // NOTE that the $i is used, not %i. This code is NOT in a function - it is inline,
  141.   // so we need to use the $ (global scope) and not the % (local scope).
  142.   for ($i = 1;  $i  < $Game::argc; $i++)
  143.   {
  144.      if (!$argumentFlag[$i])
  145.         Error("Error: Unknown command line argument:  " @ $Game::argv[$i]);
  146.   }
  147.  
  148.   if  (!$logModeEnabled)
  149.   {
  150.      SetLogMode(6);      //  Default to  a new logfile each session.
  151.   }
  152.   //  Set the add-on path list to specify the directories that will be
  153.   //  available to the scripts and engine. note that *all* required
  154.   //  directory trees are included: common and control as well as the
  155.   //  user add-ons.
  156.   $pathList = $addonList !$= "" ? $addonList @ ";control;common" : "control;common";
  157.   SetModPaths($pathList);
  158.  
  159.   // Execute startup script for the common code modules
  160.   Exec("common/main.cs");
  161.  
  162.   // Execute startup script for the control specific code modules
  163.   Exec("control/main.cs");
  164.  
  165.   // Execute startup scripts for all user add-ons
  166.   Echo("--------- Loading Add-ons ---------");
  167.   LoadAddOns($addonList);
  168.   Echo("Engine initialization complete.");
  169.  
  170.   OnStart();
  171. }
  172.